﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace WPFExcelReport.Helpers
{
    /// <summary>
    /// VisualHelper convert the given visual content as an image
    /// </summary>
    public static partial class Helper
    {
        public static string GeStrDate( string src)
        {
            string str1 = "";
            if (src.Trim().Length != 8)
            {
                str1 = src;
            }
            else
            {
                str1 = src.Substring(6, 2) + "." + src.Substring(4, 2) + "." + src.Substring(2, 2);
            }
            return str1;
           
        }
        public static string ParseSectionStr(string sections)
        { 
            string ex="";
            string[]split=sections.Split(new char[] { ';' });
            foreach (string s in split)
            {
                ex += "'" + s + "',";
            }
            ex += "***";
            ex = ex.Replace(",***", "");
            return ex;
        }
        public static string GetMaxRowNum(DataTable tb,string kf,string key,string pos)
        {
            string rownum = "";
            var rm = (from DataRow row in tb.AsEnumerable()
                      where row[kf].ToString()==key
                          select row[pos])
                      .Max();
            if (rm == null)
                rm = 0;
            rownum=Right("000" + (System.Convert.ToInt64(rm.ToString()) + 10).ToString(), 3);
            return rownum;
        }
        public static string GetQPTerm(DataSet dsSource, string key)
        {
            key = "00" + Left(key + "                                           ", 23);
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0477"
                      && row["SDKEY"].ToString().Trim() == key.Trim()
                      select row;
            foreach (DataRow row in qpr)
            {
                key = row["SDDATEN"].ToString().Substring(1, 50).Trim();
            }
            return GetGB2312Value(key).Trim();

        }
        public static string Get0312V(DataSet dsSource, string key)
        {
            string KEY = "";
            string result = key;
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0312"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {

                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            return result;

        }
        public static string Get0196V(DataSet dsSource, string key)
        {
            string KEY = "";
            string result = key;       
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0196"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {
                
                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            return result;

        }
        public static string Get0178V(DataSet dsSource, string key)
        {
            string KEY = "";
            string result = key;
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0178"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {
                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            return result;

        }
        public static string Get0255V(DataSet dsSource, string key)
        {
            string KEY = "";
            string result = key;
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0255"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {

                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            return result;
        }

        public static string Get0649V(DataSet dsSource, string key)
        {
            string KEY = "";
            string result = key;
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0649"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {
                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            return result; 
        }
        public static string GetMukeup(DataSet dsSource, string key)
        {
            key = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0114"
                      && row["SDKEY"].ToString() == key
                      select row;
            foreach (DataRow row in qpr)
            {
                key = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            return key;
     
        }
        public static string GetQATerm(DataSet dsSource, string key)
        {
            key = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0189"
                      && row["SDKEY"].ToString() == key
                      select row;
            foreach (DataRow row in qpr)
            {
                key = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            return GetGB2312Value(key);

        }
        public static string GetDiscountTerm(DataSet dsSource, string key)
        {
            string result = "";
            string KEY = "";
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0243"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {
                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            if (result.Trim() == "")
            {
                KEY = "00" + (key + "                                     ").Substring(0, 21) + "E";
                qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                          where row["SDSA"].ToString() == "0243"
                          && row["SDKEY"].ToString() == KEY
                          select row;
                foreach (DataRow row in qpr)
                {
                    result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
                }
            }
            if (result.Trim() == "")
                result = key;
            return GetGB2312Value(result);
        }
        public static string GetAddTerm(DataSet dsSource, string key)
        {
            string result = "";
            string KEY = "";
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0255"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {
                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            if (result.Trim() == "")
            {
                KEY = "00" + (key + "                                     ").Substring(0, 21) + "E";
                qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0255"
                      && row["SDKEY"].ToString() == KEY
                      select row;
                foreach (DataRow row in qpr)
                {
                    result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
                }
            }
            if (result.Trim() == "")
                result = key;
            return GetGB2312Value(result);
        }
        public static string GetDelTerm(DataSet dsSource, string key)
        {
            string result = "";
            string KEY = "";
            KEY = "00" + (key + "                                     ").Substring(0, 21) + "V";
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0241"
                      && row["SDKEY"].ToString() == KEY
                      select row;
            foreach (DataRow row in qpr)
            {
                result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
            }
            if (result.Trim() == "")
            {
                KEY = "00" + (key + "                                     ").Substring(0, 21) + "E";
                qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0241"
                      && row["SDKEY"].ToString() == KEY
                      select row;
                foreach (DataRow row in qpr)
                {
                    result = row["SDDATEN"].ToString().Substring(1, 20).Trim();
                }
            }
            if (result.Trim() == "")
                result = key;
            return GetGB2312Value(result);
        }
        public static string GetWeave(DataSet dsSource, string key)
        {
            key = "00" + Left(key + "                                           ", 23);
            var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                      where row["SDSA"].ToString() == "0734"
                      && row["SDKEY"].ToString().Trim() == key.Trim()
                      select row;
            foreach (DataRow row in qpr)
            {
                key = row["SDDATEN"].ToString().Substring(51, 10).Trim();
            }
            return GetGB2312Value(key).Trim();

        }
        public static string GetHRName(DataSet dsSource, string key)
        {

            var qpr = from DataRow row in dsSource.Tables["HRSTAMM"].AsEnumerable()
                      where row["HRBENID"].ToString().Trim() == key.Trim()
                      select row;
            foreach (DataRow row in qpr)
            {
                key = row["HRNNAME"].ToString().Substring(0, 6).Trim();
            }
            return GetGB2312Value(key).Trim();
        }
        public static string GetCompositionVA(DataSet dsSource,DataRow dwVA07)
        {
            string com = "";
            int i = 1;
            while (true)
            {
                if (i > 8)
                    break;
                string cx = dwVA07["VAAGESANT" + i].ToString();
                string pr = dwVA07["VAAGESBEZ" + i].ToString();
                string key;

                if (cx.Trim() == "000")
                    break;
                if (pr.Trim() == "")
                    break;
                key = "00" + Left(pr + "                                               ", 21) + "V";
                var qpr = from DataRow row in dsSource.Tables["SDDAT"].AsEnumerable()
                          where row["SDSA"].ToString() == "0013"
                          && row["SDKEY"].ToString() == key
                          select row;
                foreach (DataRow row in qpr)
                {
                    pr = row["SDDATEN"].ToString().Substring(1, 40).Trim();
                }
                com += System.Convert.ToInt16(cx.Trim()) + "%" + pr.Trim();
                i++;
            }
            return com;
        }
        public static DataSet ConvertTXT(DataTable tbTXT,DataRow dwTarget)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet(); 
                Dictionary<string, int> txtID = new Dictionary<string, int>();
                string processed = "";
                string TXTISIND = "";
                string TXTKPOS = "";
                DataRow dwTXTKON = null;
                DataTable tbTXTKON = dwTarget.Table.Clone();
                int pos = 1;
                foreach (DataRow dwTXT in tbTXT.Rows)
                {
                    TXTISIND = dwTXT["TXTISIND"].ToString();
                    TXTKPOS = dwTXT["TXTKPOS"].ToString();
                    if (!processed.Contains(TXTISIND + ";"))
                    {
                        processed += TXTISIND + ";";
                        dwTXTKON = tbTXTKON.NewRow();
                        tbTXTKON.Rows.Add(dwTXTKON);
                        dwTXTKON["TXTIPIND"] = TXTISIND;
                    }
                    else
                    {
                        var query = from DataRow dw in tbTXTKON.AsEnumerable()
                                    where dw["TXTIPIND"].ToString() == TXTISIND
                                    select dw;
                        foreach (DataRow dw in query)
                        {
                            dwTXTKON = dw;
                        }
                    }

                    if (txtID.ContainsKey(TXTISIND))
                    {
                        pos = txtID[TXTISIND];
                        pos++;
                        txtID[TXTISIND] = pos;
                    }
                    else
                    {
                        pos = 1;
                        txtID.Add(TXTISIND, pos);
                    }
                    TXTKPOS = Right("000" + pos.ToString() + "0", 3);

                    if (tbTXTKON.Columns.Contains("TXT" + TXTKPOS))
                    {
                        dwTXTKON["TXT" + TXTKPOS] = dwTXT["TXTKTEXT"];
                    }

                }
                SReturn.Merge(tbTXTKON);
           
            return SReturn;
        }
        public static string GetSubRollNum(string STKNR)
        {
            int lg = STKNR.Length;
            int first1 = STKNR.IndexOf("-");
            if (first1 > 0)
            {
                STKNR = STKNR.Substring(first1,lg-first1);
            }
            first1 = STKNR.IndexOfAny(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            if (first1 > 0)
            {
                STKNR = STKNR.Substring(first1);
            }
            return STKNR.Trim();
        }
        public static string GetAbsRollNum(string STKNR,string cardno)
        {
            STKNR = "*" + STKNR;
            cardno = "*" + cardno;
            STKNR = STKNR.Replace(cardno, "");
            STKNR = GetSubRollNum(STKNR);
            return STKNR.Trim();
        }
        public static void GetDate(ref string date, ref string year, ref string month, ref string day)
        {
            DateTime now = DateTime.Now;
            year = now.Year.ToString();
            month = Right("00" + now.Month.ToString(), 2);
            day = Right("00" + now.Day.ToString(), 2);
            date = year + month + day;
        }
        public static void GetSplitDate(ref string str1,ref string str2, ref string str3,string src)
        {
            if (src.Trim().Length != 8)
            {
                str1 = src;
                str2 = src;
                str3 = src;
                return;
            }
            str1 = src.Substring(0, 4) + "年" + src.Substring(4, 2) + "月" + src.Substring(6, 2)+"日";
            str3 = src.Substring(0, 4) + "/" + src.Substring(4, 2) + "/" + src.Substring(6, 2);
            str2 = src.Substring(4, 2) + "月" + src.Substring(6, 2)+"日";
        }
        public static string GetComposition(string art,DataTable tbStueli)
        {
            string composition = "";
            
            foreach (DataRow row in tbStueli.Rows)
            {
                string cond = row["C"].ToString().Trim();
                if (cond != art.Trim())
                    continue;
                composition += row["B"].ToString().Trim()+";";
            }
            composition += "***";
            composition = composition.Replace(";***", "");
            return composition;
        }
        public static void SplitString(ref string src,ref string str1,ref string str2)
        {
            int idx = src.IndexOfAny(new char[] { ':', '：'});
            if (idx > 0)
            {
                str1 = src.Substring(0, idx);
                str2 = src.Substring(idx+1);
            }
        }
    }
}
